package com.atmilan.quartz.utils.product;

import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.config.RestTemplateConfig;
import com.atmilan.quartz.model.dto.vietti.ViettiProductResDTO;
import com.atmilan.quartz.model.dto.vietti.ViettiProductSkuRes;
import com.atmilan.quartz.model.dto.vietti.ViettiProductSkuResDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * 拉取商品源数据
 */

@Slf4j
@Service
public class ViettiProductUtil {

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    @Value("${vietti.client}")
    private String viettiClient;
    @Value("${vietti.appSecret}")
    private String viettiAppSecret;
    @Value("${vietti.appkey}")
    private String viettiAppKey;
    @Value("${vietti.url.domain}")
    private String viettiDomian;
    @Value("${vietti.url.products}")
    private String viettiProducts;
    @Value("${vietti.url.stocks}")
    private String viettiStocks;

    /**
     * 查询分页商品数据
     * @Description:
     * @author daigb
     * @param @param pageNow
     * @param @param pageSize
     * @param @return
     * @param @throws Exception
     * @return InProductResDTO
     * @throws
     */
    public ViettiProductResDTO selectViettiPage() throws Exception {
        ViettiProductResDTO viettiProductResDTO = null;
        try {
            // 组装请求url
            StringBuilder sb = new StringBuilder();
            sb.append(viettiDomian);
            sb.append("/");
            sb.append(viettiClient);
            sb.append(viettiProducts);
            sb.append("?channelKey=");
            sb.append(viettiAppSecret);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("Ocp-Apim-Subscription-Key", viettiAppKey);
            HttpEntity<String> entity = new HttpEntity<String>(headers);
            ResponseEntity<String> response = restTemplate.exchange(sb.toString(), HttpMethod.GET, entity, String.class);

            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- vietti查询分页商品数据异常{} ---------------", errorMessage);
            }

            ObjectMapper mapper = new ObjectMapper();
            viettiProductResDTO = mapper.readValue(response.getBody(), ViettiProductResDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return viettiProductResDTO;
    }

    /**
     * 获取单个商品sku
     * @param productId
     * @return
     */
    public ViettiProductSkuRes getViettiSku(String productId) {
        ViettiProductSkuRes viettiProductSkuRes = null;
        try {
            // 组装请求url
            StringBuilder sb = new StringBuilder();
            sb.append(viettiDomian);
            sb.append("/");
            sb.append(viettiClient);
            sb.append(viettiProducts)
                    .append("/").append(productId);
            sb.append("?channelKey=");
            sb.append(viettiAppSecret);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("Ocp-Apim-Subscription-Key", viettiAppKey);
            HttpEntity<String> entity = new HttpEntity<String>(headers);
            ResponseEntity<String> response = restTemplate.exchange(sb.toString(), HttpMethod.GET, entity, String.class);
            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- Vietti获取单个商品sku异常{} ---------------", errorMessage);
            }
            ObjectMapper mapper = new ObjectMapper();
            viettiProductSkuRes = mapper.readValue(response.getBody(), ViettiProductSkuRes.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return viettiProductSkuRes;
    }

    /**
     * 获取商品sku
     * @Description:
     * @author daigb
     * @param @param ids
     * @param @return
     * @param @throws Exception
     * @return InProductResDTO
     * @throws
     */
    public ViettiProductSkuResDTO getViettiSkus() throws Exception {
        // 组装请求url
        ViettiProductSkuResDTO viettiProductSkuResDTO = null;
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(viettiDomian);
            sb.append("/");
            sb.append(viettiClient);
            sb.append(viettiStocks);
            sb.append("?channelKey=");
            sb.append(viettiAppSecret);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("Ocp-Apim-Subscription-Key", viettiAppKey);
            HttpEntity<String> entity = new HttpEntity<String>(headers);
            ResponseEntity<String> response = restTemplate.exchange(sb.toString(), HttpMethod.GET, entity, String.class);

            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- Vietti获取商品sku异常{} ---------------", errorMessage);
            }

            ObjectMapper mapper = new ObjectMapper();
            viettiProductSkuResDTO = mapper.readValue(response.getBody(), ViettiProductSkuResDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return viettiProductSkuResDTO;
    }

}
